package com.chujian.system.auth.controller;

import com.chujian.framework.core.base.TsDataController;
import com.chujian.framework.core.tsdata.Result;
import com.chujian.login.vo.LoginResult;
import com.chujian.login.vo.PcLoginInfo;
import com.chujian.wx.constant.ChujianWxConst;
import com.chujian.wx.vo.GetMaUserInfoVo;
import com.chujian.system.auth.service.ILoginService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;


@RestController
public class LoginController extends TsDataController {

    @Resource
    private ILoginService loginService;

    /**
     * pc登录
     *
     * @param loginInfo loginInfo
     * @return
     */
    @PostMapping("/login/pc")
    public Result<?> login(@RequestBody PcLoginInfo loginInfo) {
        LoginResult loginResult = loginService.pcLogin(loginInfo);
        return okRs(loginResult);
    }

    /**
     * 小程序登录
     *
     * @param appId
     * @param getMaUserInfoVo
     * @return
     */
    @PostMapping("/login/ma")
    public Result<?> loginMa(@RequestHeader(ChujianWxConst.APP_ID_HEADER) String appId, @RequestBody GetMaUserInfoVo getMaUserInfoVo) {
        LoginResult loginResult = loginService.loginMa(appId, getMaUserInfoVo);
        return okRs(loginResult);
    }

    /**
     * 尝试生成小程序token
     *
     * @param appId
     * @param openId
     * @param userLoginId
     * @param prodId      产品ID
     * @param t           请求发送时间，单位秒
     * @return
     */
    @PostMapping("/open/ma/tryGetMaToken")
    public Result<?> tryGetMaToken(
            @RequestHeader(ChujianWxConst.APP_ID_HEADER) String appId,
            @RequestParam("openId") String openId,
            @RequestParam("userLoginId") Long userLoginId,
            @RequestParam("prodId") Long prodId,
            @RequestParam("_t") Long t
    ) {
        LoginResult loginResult = loginService.tryGetMaToken(appId, openId, userLoginId, prodId, t);
        return okRs(loginResult);
    }

}
